package com.boang.service;

import com.boang.entity.Cla;
import com.boang.entity.Student;
import com.boang.util.DbUtil;
import com.boang.util.StringToLocalDateTime;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
    @author:sa
    @date:2024/12/11 11:39
*/
public class StuService {
    public List<Student> list() throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("select * from student");
        ResultSet resultSet = preparedStatement.executeQuery();
        ArrayList<Student> list = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String sex = resultSet.getString("sex");
            String birthday = resultSet.getString("birthday");
            LocalDateTime birthdayLdt = StringToLocalDateTime.parse(birthday);
            Double fee = resultSet.getDouble("fee");
            String grad = resultSet.getString("grad");
            int score = resultSet.getInt("score");
            String gmtCreate = resultSet.getString("gmt_create");
            LocalDateTime gmtCreateLdt = StringToLocalDateTime.parse(gmtCreate);
            String gmtModified = resultSet.getString("gmt_modified");
            LocalDateTime gmtModifiedLdt = StringToLocalDateTime.parse(gmtModified);
            Student student = new Student(id,name,age,sex,birthdayLdt,fee,grad,score,gmtCreateLdt,gmtModifiedLdt);
        list.add(student);
        }
        return list;
    }
    public int addStu(Student student) throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("insert into student(id,name,age,sex,birthday,fee,grad,score)values(?,?,?,?,?,?,?,?)");
        preparedStatement.setInt(1, student.getId());
        preparedStatement.setString(2, student.getName());
        preparedStatement.setInt(3, student.getAge());
        preparedStatement.setString(4, student.getSex());
        preparedStatement.setString(5, String.valueOf(student.getBirthday()));
        preparedStatement.setDouble(6, student.getFee());
        preparedStatement.setString(7, student.getGrad());
        preparedStatement.setInt(8, student.getScore());
        return preparedStatement.executeUpdate();
    }
    public int removeStu(int id) throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("delete from student where id = ?");
        preparedStatement.setInt(1, id);
        return preparedStatement.executeUpdate();
    }
    public int updetaStu(Student student) throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("update student set name=? where id = ?");
        preparedStatement.setString(1, student.getName());
        preparedStatement.setInt(2, student.getId());
        return preparedStatement.executeUpdate();
    }
    }